package com.achievement.controller;

import com.achievement.domain.dto.BookDTO;
import com.achievement.domain.param.BookParam;
import com.achievement.domain.vo.BookVO;
import com.achievement.service.IBookService;
import com.common.annotation.Anonymous;
import com.common.annotation.Log;
import com.common.core.controller.BaseController;
import com.common.core.domain.AjaxResult;
import com.common.core.page.TableDataInfo;
import com.common.enums.BusinessType;
import com.common.utils.poi.ExcelUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.List;

/**
 * BookContrelloer
 */


@RestController
@RequestMapping("/achievement/book")
@Api(tags = "成果管理模块---学术著作")
public class BookContrelloer extends BaseController {

    @Autowired
    private IBookService bookService;

    /**
     * 新增著作基本信息及作者信息
     */
    @PreAuthorize("@ss.hasPermi('achievement:book:add')")
    @Log(title = "新增著作基本信息及作者信息", businessType = BusinessType.INSERT)
    @ApiOperation(value = "新增著作基本信息及作者信息",notes = "新增著作基本信息及作者信息")
    @PostMapping(value = "/add")
    public AjaxResult add(@RequestBody BookDTO dto)
    {
        return bookService.add(dto);
    }

    /**
     * 删除著作
     */
    @PreAuthorize("@ss.hasPermi('achievement:book:remove')")
    @Log(title = "删除著作", businessType = BusinessType.DELETE)
    @ApiOperation(value = "删除著作",notes = "删除著作")
    @DeleteMapping("/del/{ids}")
    public AjaxResult remove(@PathVariable List<String> ids)
    {
        return toAjax(bookService.batchRemove(ids));
    }

    /**
     * 修改著作
     */
    @PreAuthorize("@ss.hasPermi('achievement:book:edit')")
    @Log(title = "修改著作", businessType = BusinessType.UPDATE)
    @ApiOperation(value = "修改著作",notes = "修改著作")
    @PostMapping(value = "/update")
    public AjaxResult edit(@RequestBody BookDTO dto)
    {
        return bookService.update(dto);
    }

    /**
     * 列表检索
     */
    @PreAuthorize("@ss.hasPermi('achievement:book:list')")
    @Log(title = "列表检索")
    @ApiOperation(value = "列表检索",notes = "列表检索")
    @GetMapping("/list")
    public TableDataInfo list(BookParam param)
    {
        startPage();
        List<BookVO> list = bookService.selectByPage(param);
        return getDataTable(list);
    }

    /**
     * 左上方统计 通过，退回，待审核
     * @param param 检索参数
     * @return AjaxResult
     */
    @PreAuthorize("@ss.hasPermi('achievement:book:list')")
    @ApiOperation(value = "左上方统计 通过，退回，待审核",notes = "左上方统计 通过，退回，待审核")
    @GetMapping(value = "/list-statistics")
    public AjaxResult listStatistics(BookParam param)
    {
        List<HashMap<String, Integer>> list =bookService.listStatistics(param);

        return success(list);
    }

    /**
     * 获取著作详细信息
     */
    @PreAuthorize("@ss.hasPermi('achievement:book:query')")
    @ApiOperation(value = "获取著作详细信息",notes = "获取著作详细信息")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") String id)
    {
        return success(bookService.getInfoById(id));
    }

    /**
     * 著作导出
     */
    @PreAuthorize("@ss.hasPermi('achievement:book:export')")
    @Log(title = "著作导出", businessType = BusinessType.EXPORT)
    @ApiOperation(value = "著作导出",notes = "著作导出")
    @PostMapping("/export")
    public void export(HttpServletResponse response, @RequestBody BookParam param)
    {
        List<BookVO> list = bookService.getListByConditions(param);
        List<String> selectedColumns = param.getExcelColumn();

        ExcelUtil<BookVO> util = new ExcelUtil<>(BookVO.class);
        util.showColumn(selectedColumns.toArray(new String[0]));
        util.exportExcelByOrder(response, list, "著作清单",selectedColumns.toArray(new String[0]));
    }
}
